package tools;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */





import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Alvaro
 */
@WebServlet(name="signup_process1", urlPatterns={"/signup_process1"})
public class signup_process1 extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();


        String name = request.getParameter("name");
        String lastname = request.getParameter("lastname");
        String birthday = request.getParameter("birth");
        String email = request.getParameter("email");
        String failpal = request.getParameter("failpal");
        String wisa = request.getParameter("wisa");
        String transfer = request.getParameter("transfer");
        String movements = request.getParameter("passwordmov");
        String account = request.getParameter("account");
        String cardnumber = request.getParameter("cardnumber");
        String cardsecurity = request.getParameter("cardsecurity");
        int money = 0;


        try {
           
            out.println("<html>");
            out.println("<head>");
            out.println("<title>USP SIGN UP</title>");
            out.println("<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"favicon.ico\">");
            out.println("</head>");
            out.println("<body bgcolor=\"#CCC\">");
            out.println("<h1>UPS SIGN UP PROCESS</h1>");
            out.println("<table align=center>");
            out.println("<tr align=\"center\">Name: "+ name+"</tr>");
            out.println("<br><tr align=\"center\">Second name: "+ lastname +"</tr>");
            out.println("<br><tr align=\"center\">Email: "+ email+"</tr>");
            out.println("<br><tr align=\"center\">Birth: "+ birthday+"</tr>");
            out.println("<br><tr align=\"center\">Account: "+ account+"</tr>");
            

            //out.println("</ul>");           
            out.println("</table>");
            out.println("This is your sign up information...");


            out.println("<form method=\"get\" action=\"/Payment_System-war/index.jsp\">");
            out.println("<button align=\"center\" type=\"submit\" name=\"accept\" value=\"Accpet\" >Accept</button>");
            out.println("</form>");
            
            /////////////INSERTAR USUARIOS A LA BASE DE DATOS
            
             // Insertar usuarios en la base de datos
       /* final String connURL= "jdbc:mysql://localhost:3306/payment-users";
        Connection conn = null;
         try
       {
           conn =  DriverManager.getConnection(connURL,"root","tintin");//"bys-dba","password");
           Statement stmt = conn.createStatement();
           
           
       
           String sentencia =  "INSERT INTO `payment-users`.`user` (name,lastname,birth,email,account,failpal,wisa,"
                    + "transfer,money) VALUES ('"+name+"','"+lastname+"','"+birthday+"','"+email+"','"+account+"','"+failpal+"'"
                    + ",'"+wisa+"','"+transfer+"',"+money+")";
             
           stmt.executeUpdate(sentencia);

           if (stmt != null) stmt.close();
           if (conn != null)conn.close();


        } catch (Exception e) {
            Logger.getLogger("Guardado en").log(Level.SEVERE, "ERROR: " + e.getLocalizedMessage());
            System.err.println("SQLException: " + e.getMessage());
            System.out.println("Se ha producido un Error al introducir los datos en la BBDD");
           
        }*/
    
    
    
   
            
            
            /////////////FIN INSERTAR USUARIOS A LA BASE DE DATOS

            final String url = "jdbc:mysql://localhost:3306/payment-users";
            final String usuario = "root";
            final String passwd = "tintin";


            Connection connection = null;

            try {
              Class.forName("com.mysql.jdbc.Driver");
              connection = DriverManager.getConnection(url, "root", "tintin");

              Statement insertarUsuario = connection.createStatement();


            insertarUsuario.executeUpdate
                    ("INSERT INTO `payment-users`.`user` (name,lastname,birth,email,account,failpal,wisa,"
                    + "transfer,movements,money,cardnumber,cardsecurity) VALUES ('"+name+"','"+lastname+"','"+birthday+"','"+email+"','"+account+"','"+failpal+"'"
                    + ",'"+wisa+"','"+transfer+"','"+movements+"',"+money+",'"+cardnumber+"','"+cardsecurity+"')");

            insertarUsuario.close();
            connection.close();
            } catch (SQLException e) {
              System.out.println("Error de MySQL: " + e.getMessage());
              out.println("<label>Internar error! try again later!</label>");
            } catch (Exception e) {
              System.out.println("Error inesperado: " + e.getMessage());
              out.println("<label>Internar error! try again later!</label>");
            }


            
            ////////////////////////////////////////////////////////////////////Añadimos los datos a la base de datos



           
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(signup_process1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(signup_process1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
